﻿using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace Sharp.Framework.Unitity
{
    public class WindDirectionImageUnitity
    {
        /// <summary>
        /// 生成风力风向图标
        /// </summary>
        /// <param name="dd">风向</param>
        /// <param name="df">风速</param>
        /// <param name="color"> </param>
        /// <param name="filePath"> </param>
        /// <returns></returns>
        public static string BuildFImage(int dd, double df, string color, string filePath)
        {
            string imageName;//图片名称
            var fj = 0;//风级别
            Bitmap bitmap;
            Graphics g;

            bitmap = new Bitmap(30, 30);
            g = Graphics.FromImage(bitmap);
            g.Clear(Color.Transparent);
            var c = Color.FromArgb(255, 8, 175, 26);
            switch (color)
            {
                case "08AF1A":
                    c = Color.FromArgb(255, 8, 175, 26);
                    break;
                case "B232A1":
                    c = Color.FromArgb(255, 178, 50, 161);
                    break;
            }
            Pen p = new Pen(c, 1);

            if (df <= 0.2)
            {
                fj = 0;
                g.DrawLine(p, 0, 0, 0, 29);//风向
            }
            else if (1.5 >= df && df >= 0.3)
            {
                fj = 1;
                g.DrawLine(p, 0, 0, 0, 29);//风向
                //风速
                g.DrawLine(p, 0, 29, 5, 29);
            }
            else if (3.3 >= df && df >= 1.6)
            {
                fj = 2;
                g.DrawLine(p, 0, 0, 0, 29);//风向
                //风速
                g.DrawLine(p, 0, 29, 10, 29);
            }
            else if (5.4 >= df && df >= 3.4)
            {
                fj = 3;
                g.DrawLine(p, 0, 0, 0, 29);//风向
                //风速
                g.DrawLine(p, 0, 29, 10, 29);
                g.DrawLine(p, 0, 25, 5, 25);
            }
            else if (7.9 >= df && df >= 5.5)
            {
                fj = 4;
                g.DrawLine(p, 0, 0, 0, 29);//风向
                //风速
                g.DrawLine(p, 0, 29, 10, 29);
                g.DrawLine(p, 0, 25, 10, 25);
            }
            else if (10.7 >= df && df >= 8.0)
            {
                fj = 5;
                g.DrawLine(p, 0, 0, 0, 29);//风向
                //风速
                g.DrawLine(p, 0, 29, 10, 29);
                g.DrawLine(p, 0, 25, 10, 25);
                g.DrawLine(p, 0, 20, 5, 20);
            }
            else if (13.8 >= df && df >= 10.8)
            {
                fj = 6;
                g.DrawLine(p, 0, 0, 0, 29);//风向
                //风速
                g.DrawLine(p, 0, 29, 10, 29);
                g.DrawLine(p, 0, 25, 10, 25);
                g.DrawLine(p, 0, 20, 10, 20);
            }
            else if (17.1 >= df && df >= 13.9)
            {
                fj = 7;
                g.DrawLine(p, 0, 0, 0, 29);//风向
                //风速
                g.DrawLine(p, 0, 29, 10, 29);
                g.DrawLine(p, 0, 25, 10, 25);
                g.DrawLine(p, 0, 20, 10, 20);
                g.DrawLine(p, 0, 15, 5, 15);
            }
            else if (20.7 >= df && df >= 17.2)
            {
                fj = 8;
                g.DrawLine(p, 0, 0, 0, 29);//风向
                //风速
                Point[] pntArr = new Point[3];
                pntArr[0] = new Point(0, 29);
                pntArr[1] = new Point(10, 29);
                pntArr[2] = new Point(0, 25);
                g.DrawPolygon(p, pntArr);
            }
            else if (24.4 >= df && df >= 20.8)
            {
                fj = 9;
                g.DrawLine(p, 0, 0, 0, 29);//风向
                //风速
                Point[] pntArr = new Point[3];
                pntArr[0] = new Point(0, 29);
                pntArr[1] = new Point(10, 29);
                pntArr[2] = new Point(0, 25);
                g.DrawPolygon(p, pntArr);

                g.DrawLine(p, 0, 25, 5, 25);

            }
            else if (28.4 >= df && df >= 24.5)
            {
                fj = 10;
                g.DrawLine(p, 0, 0, 0, 29);//风向
                //风速
                Point[] pntArr = new Point[3];
                pntArr[0] = new Point(0, 29);
                pntArr[1] = new Point(10, 29);
                pntArr[2] = new Point(0, 25);
                g.DrawPolygon(p, pntArr);
                g.DrawLine(p, 0, 25, 10, 25);

            }
            else if (32.6 >= df && df >= 28.5)
            {
                fj = 11;
                g.DrawLine(p, 0, 0, 0, 29);//风向
                //风速
                Point[] pntArr = new Point[3];
                pntArr[0] = new Point(0, 29);
                pntArr[1] = new Point(10, 29);
                pntArr[2] = new Point(0, 25);
                g.DrawPolygon(p, pntArr);
                g.DrawLine(p, 0, 25, 10, 25);
                g.DrawLine(p, 0, 20, 5, 20);
            }
            else if (df >= 32.7)
            {
                fj = 12;
                g.DrawLine(p, 0, 0, 0, 29);//风向
                //风速
                Point[] pntArr = new Point[3];
                pntArr[0] = new Point(0, 29);
                pntArr[1] = new Point(10, 29);
                pntArr[2] = new Point(0, 25);
                g.DrawPolygon(p, pntArr);
                g.DrawLine(p, 0, 25, 10, 25);
                g.DrawLine(p, 0, 20, 10, 20);
            }
            imageName = fj + "_" + dd + "_" + color;
            RotateImg(bitmap, dd).Save(string.Format("{0}/{1}.png", filePath, imageName), ImageFormat.Png);

            bitmap.Dispose();
            g.Dispose();
            return imageName + ".png";
        }
        /// <summary>
        /// 以逆时针为方向对图像进行旋转
        /// </summary>
        /// <param name="b">位图流</param>
        /// <param name="angle">旋转角度[0,360](前台给的)</param>
        /// <returns></returns>
        public static Image RotateImg(Image b, int angle)
        {
            angle = angle % 360;
            //弧度转换
            double radian = angle * Math.PI / 180.0;
            double cos = Math.Cos(radian);
            double sin = Math.Sin(radian);
            //原图的宽和高
            int w = b.Width;
            int h = b.Height;
            var W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
            var H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));

            //目标位图
            Bitmap dsImage = new Bitmap(W, H);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
            //计算偏移量
            Point Offset = new Point((W - w) / 2, (H - h) / 2);
            //构造图像显示区域：让图像的中心与窗口的中心点一致
            Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
            Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
            g.TranslateTransform(center.X, center.Y);
            g.RotateTransform(360 - angle);
            //恢复图像在水平和垂直方向的平移
            g.TranslateTransform(-center.X, -center.Y);
            g.DrawImage(b, rect);
            //重至绘图的所有变换
            g.ResetTransform();
            g.Save();
            g.Dispose();
            //保存旋转后的图片
            b.Dispose();
            return dsImage;
        }
    }
}